package com.crl.utils.color
{

	
	
	/**
	 *  颜色表式转换;
	 * 
	 *  具体的算法可参考 :http://www.codeproject.com/KB/recipes/colorspace1.aspx;
	 * 	 色彩表示图,可预览assets/colors 底下的文件;
	 * 
	 * 
	 * @author crl
	 * 
	 */	
	public class ConvertColor
	{
		/**
		 *  由rgb 三个色调 调出 颜色;
		 * @param r
		 * @param g
		 * @param b
		 * @return 
		 * 
		 */		
		public static function RGBToHex(rgb:RGB):uint
		{
			var hex:uint = rgb.r<<16 | rgb.g<<8 | rgb.b;
			return hex;
		}
		
		
		/**
		 * 由颜色值取出rgb 各分部; 
		 * @param value
		 * @return 
		 * 
		 */		
		public static function HexToRGB(value:uint):RGB{
			var rgb:RGB = new RGB();
			rgb.r = (value >> 16) & 0xFF
			rgb.g = (value >> 8) & 0xFF
			rgb.b = value & 0xFF			
			return rgb;
		}
		
		/**
		 * RGBからHSBへ変換
		 * @param r
		 * @param g
		 * @param b
		 * @return 
		 * 
		 */		
		public static function RGBToHSB(rgb:RGB):HSB{
			
			var r:int=rgb.r;
			var g:int=rgb.g;
			var b:int=rgb.b;
			
			
			var hsb:HSB = new HSB;
			
			//取得rgb 中 最大与最小的值;
			var _max:Number = Math.max(r,g,b);
			var _min:Number = Math.min(r,g,b);
			
			// 彩度
			hsb.s = (_max != 0) ? (_max - _min) / _max * 100: 0;
			// 明度
			hsb.b = _max / 255 * 100;
			// 色相
			if(hsb.s == 0){
				hsb.h = 0;
			}else{
				switch(_max)
				{
					case r:
						hsb.h = (g - b)/(_max - _min)*60 + 0;
						break;
					case g:
						hsb.h = (b - r)/(_max - _min)*60 + 120;
						break;
					case b:
						hsb.h = (r - g)/(_max - _min)*60 + 240;
						break;
					default :
						break;
				}
			}
			
			hsb.h = Math.min(360, Math.max(0, Math.round(hsb.h)))
			hsb.s = Math.min(100, Math.max(0, Math.round(hsb.s)))
			hsb.b = Math.min(100, Math.max(0, Math.round(hsb.b)))
			
			return hsb;
		}
		
		
		/**
		 * HSB到RGB的转換 
		 * @param h
		 * @param s
		 * @param b
		 * @return 
		 * 
		 */		
		public static function HSBToRGB(hsb:HSB):RGB{
			
			var h:int=hsb.h;
			var s:int=hsb.s;
			var b:int=hsb.b;
			
			
			var rgb:RGB = new RGB();
			
			var max:Number = (b*0.01)*255;
			var min:Number = max*(1-(s*0.01));
			
			if(h == 360){
				h = 0;
			}
			
			if(s == 0){
				rgb.r = rgb.g = rgb.b = b*(255*0.01) ;
			}else{
				var _h:Number = Math.floor(h / 60);
				
				switch(_h){
					case 0:
						rgb.r = max	;																																																					
						rgb.g = min+h * (max-min)/ 60;
						rgb.b = min;
						break;
					case 1:
						rgb.r = max-(h-60) * (max-min)/60;
						rgb.g = max;
						rgb.b = min;
						break;
					case 2:
						rgb.r = min ;
						rgb.g = max;
						rgb.b = min+(h-120) * (max-min)/60;
						break;
					case 3:
						rgb.r = min;
						rgb.g = max-(h-180) * (max-min)/60;
						rgb.b =max;
						break;
					case 4:
						rgb.r = min+(h-240) * (max-min)/60;
						rgb.g = min;
						rgb.b = max;
						break;
					case 5:
						rgb.r = max;
						rgb.g = min;
						rgb.b = max-(h-300) * (max-min)/60;
						break;
					case 6:
						rgb.r = max;
						rgb.g = min+h  * (max-min)/ 60;
						rgb.b = min;
						break;
				}
				
				rgb.r = Math.min(255, Math.max(0, Math.round(rgb.r)))
				rgb.g = Math.min(255, Math.max(0, Math.round(rgb.g)))
				rgb.b = Math.min(255, Math.max(0, Math.round(rgb.b)))
			}
			return rgb;
		}
		
		
		/**
		 * 
		 * @param r
		 * @param g
		 * @param b
		 * @return 
		 * 
		 */		
		public static function RGBToCMY(rgb:RGB):CMY{
			var ratio:Number = 100/255;
			
			var cmy:CMY = new CMY();
			cmy.c = Math.round(100-rgb.r*ratio);
			cmy.m = Math.round(100-rgb.g*ratio);
			cmy.y = Math.round(100-rgb.b*ratio);
			
			return cmy;
		}
		
		public static function CMYKToRGB(cmy:CMY,k:uint = 0):RGB{
			k = 1-k/100;
			
			var rgb:RGB=new RGB();
			rgb.r=(255-cmy.c*2.55)*k;
			rgb.g=(255-cmy.m*2.55)*k;
			rgb.b=(255-cmy.y*2.55)*k;
			
			return rgb;
		}
		
		
	}
}